package demo;

public class MyCircularQueue {
    //用数组解决队列问题在于少用一个数组存储单元
    private int[]arr;
    private int maxSize;
    private int front;
    private int rear;

    public MyCircularQueue(int k) {
        arr=new int[k+1];
        //要少用一个存储单元，就要让数组maxSize比要存储的多一个
        maxSize=k+1;
        front=0;
        rear=0;
//空队列判断条件 front==rear
//满队列判断条件 front=(rear+1+maxSize)%maxSize
    }

    public boolean enQueue(int value) {
        //入队列之前判断是否为满
        if(isFull()){
            return false;
        }
        arr[rear]=value;
        //当对队头指针或者队尾指针操作时，记得注意
// rear=(rear+1+maxSize)%maxSize
        rear=(rear+1+maxSize)%maxSize;
        return true;

    }

    public boolean deQueue() {
        //出队列之前判断是否为空
        if(isEmpty()){
            return false;
        }
        front=(front+1+maxSize)%maxSize;
        return true;

    }
    public int Front() {
        if(isEmpty()){
            return -1;
        }

        return arr[front];

    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return arr[(rear-1+maxSize)%maxSize];

    }

    public boolean isEmpty() {
        return  front==rear;
    }

    public boolean isFull() {

        return front==(rear+1)%maxSize;

    }
}